<?php
namespace app\staff\controller;

use think\Request;
use think\Db;
use think\Session;
use think\Controller;
use think\Config;
use Godok\Org\Auth;
use think\Log;

class Agent extends Controller
{
	// 全部商品分类 
	function goodsList(){
		$list = Db::name('goods_category')->where('cate_status',1)->order('listorder asc')->select();
		$tree = self::getTree($list,0);
		foreach ($tree as $key => $value) {
			foreach ($value['list'] as $ke => $val) {
				foreach ($val['list'] as $k => $v) {
					$res = Db::name('goods')->where('category_id',$v['cate_id'])->select();
					if(!$res){
						unset($tree[$key]['list'][$ke]['list'][$k]);
					}
				}
				sort($tree[$key]['list'][$ke]['list']);
			}
		}
		$this->success('success','',$tree);
	}

	//根据分类id获取商品
	function goods(){
		$cate_id = Request()->param('cate_id');
		$list = Db::table('sdzx_goods')
		->field(['goods_id,goods_name,picture,brand_id,brand_name,unit'])
		->where('category_id',$cate_id)
		->order('sort asc')
		->select();
		foreach ($list as $key => $value) {
			$list[$key]['specval'] = Db::table('sdzx_goods_specval')
			->field(['specval_id,price,unit,unit_price,spec_name,specval_name'])
			->where('goods_id',$value['goods_id'])
			->select();

			//规格价--最小
			$min = Db::table('sdzx_goods_specval')
			->where('goods_id',$value['goods_id'])
			->min('price');

			//规格价--最大
			$max = Db::table('sdzx_goods_specval')
			->where('goods_id',$value['goods_id'])
			->max('price');

			if(count($list[$key]['specval'])==1){
				//单规格
				$list[$key]['unit'] = $list[$key]['specval'][0]['unit'];
				$list[$key]['region'] = $list[$key]['specval'][0]['price'];
			}else{
				//多规格
				$list[$key]['unit'] = $value['unit'];
				$list[$key]['region'] = $min.'-'.$max;
			}
		}
		if($list){
			$this->success('success','',$list);
		}else{
			$this->error('error');
		}
	}

	//商品详情
	function detailGood(){
		$goods_id = Request()->param('goods_id');

		$data = Db::table('sdzx_goods')->alias('g')
		->field('g.goods_name,g.description,g.picture,g.img_lists,g.brand_name')
		->join('sdzx_goods_specval gs','gs.goods_id=g.goods_id')
		->where('g.goods_id',$goods_id)
		->find();

		$data['description'] = htmlspecialchars_decode($data['description']);
		//商品规格
		$data['specval'] = Db::name('goods_specval')->field(['specval_id','price','spec_name','specval_name','unit_price','unit'])->where(['goods_id'=>$goods_id,'status'=>1])->select();

		//多图片
		$data['img_list'] = explode(',',$data['img_lists']);
		if($data){
			$this->success('success','',$data);
		}else{
			$this->error('error');
		}
	}

	/**
     * 商品搜索
     * @return [type] [description]
     */
	public function search()
	{
		$kw = Request()->param('keywords');

		$list = Db::table('sdzx_goods')->alias('g')
		->field('g.goods_id,g.goods_name,g.unit,g.category_id,g.category_id_1,g.category_id_2,g.category_id_3,g.keywords,g.introduction,g.sort,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion, b.brand_name')
		->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
		->join('sdzx_goods_brand b','g.brand_id=b.brand_id','left')
		->where("sval.status=1")
		->order('g.category_id_3 asc')
		->group('g.goods_id')
		->where("g.goods_name like '%{$kw}%' or g.keywords like '%{$kw}%' or b.brand_name  like '%{$kw}%' ")
		->select();
		if($list){
        	$this->success('查找成功','',$list);
		}else{
			$this->error('未查到任何商品');
		}
    }

    // 商家信息
    function shopInfo(){
    	$user_id = Request()->param('user_id');
    	$res = Db::table('sdzx_shop')->alias('s')
    	->field('s.shop_name,u.username')
    	->join('sdzx_user u','u.id=s.user_id')
    	->where('user_id',$user_id)
    	->find();
    	if($res){
    		$this->success('查找成功','',$res);
    	}else{
    		$this->error('查找失败');
    	}
    }

	// 递归分级
    public function getTree($data,$pid){
    	$tree = '';
    	foreach($data as $k => $v){
    		if($v['cate_pid'] == $pid){        
    			$v['list'] = self::getTree($data, $v['cate_id']);
				$tree[] = $v;						//unset($data[$k]);
			}
		}
		return $tree;
	}
}

